package com.ruoyi.web.controller.lowcode;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lowcode.domain.vo.LcDirectoryVo;
import com.ruoyi.lowcode.domain.bo.LcDirectoryBo;
import com.ruoyi.lowcode.service.ILcDirectoryService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 目录
 *
 * @author ruoyi
 * @date 2023-10-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/lowcode/directory")
public class LcDirectoryController extends BaseController {

    private final ILcDirectoryService iLcDirectoryService;

    /**
     * 查询目录列表
     */
    @SaCheckPermission("lowcode:directory:list")
    @GetMapping("/list")
    public TableDataInfo<LcDirectoryVo> list(LcDirectoryBo bo, PageQuery pageQuery) {
        return iLcDirectoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出目录列表
     */
//    @SaCheckPermission("lowcode:directory:export")
//    @Log(title = "目录", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(LcDirectoryBo bo, HttpServletResponse response) {
//        List<LcDirectoryVo> list = iLcDirectoryService.queryList(bo);
//        ExcelUtil.exportExcel(list, "目录", LcDirectoryVo.class, response);
//    }

    /**
     * 获取目录详细信息
     *
     * @param directoryId 主键
     */
    @SaCheckPermission("lowcode:directory:query")
    @GetMapping("/{directoryId}")
    public R<LcDirectoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long directoryId) {
        return R.ok(iLcDirectoryService.queryById(directoryId));
    }

    /**
     * 新增目录
     */
    @SaCheckPermission("lowcode:directory:add")
    @Log(title = "目录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LcDirectoryBo bo) {
        return toAjax(iLcDirectoryService.insertByBo(bo));
    }

    /**
     * 修改目录
     */
    @SaCheckPermission("lowcode:directory:edit")
    @Log(title = "目录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LcDirectoryBo bo) {
        return toAjax(iLcDirectoryService.updateByBo(bo));
    }

    /**
     * 删除目录
     *
     * @param directoryIds 主键串
     */
    @SaCheckPermission("lowcode:directory:remove")
    @Log(title = "目录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{directoryIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] directoryIds) {
        return toAjax(iLcDirectoryService.deleteWithValidByIds(Arrays.asList(directoryIds), true));
    }
}
